package com.warmlight.voicepacket.widget;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.TopicBannerPagerAdapter;
import com.warmlight.voicepacket.data.RecommendData;
import com.warmlight.voicepacket.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jason on 2018/5/23.
 * banner
 */

public class BannerTopic extends RelativeLayout {

    private View mContainer;
    private ViewPager vp_banner_topic;
    private Context mContext;
    private LinearLayout ll_dots_container;
    private List<ImageView> mDotsList;
    private TopicBannerPagerAdapter mTopicBannerPagerAdapter;
//    private List<RecommendData.DataBean.BannerBean> mDataList;
    private static final long TIME = 8000L;
    final MyHandler handler = new MyHandler();

    public BannerTopic(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public BannerTopic(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public BannerTopic(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BannerTopic(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }


    private void init(){
        mContainer = View.inflate(mContext,R.layout.banner_vp_topic,this);
        vp_banner_topic = (ViewPager) mContainer.findViewById(R.id.vp_banner_topic);
        mTopicBannerPagerAdapter = new TopicBannerPagerAdapter(mContext,null);
        vp_banner_topic.setAdapter(mTopicBannerPagerAdapter);
        initDots(2);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(vp_banner_topic != null){
                    vp_banner_topic.setCurrentItem(vp_banner_topic.getCurrentItem()+1);
//                    handler.sendEmptyMessageDelayed(0,TIME);
                }else{
//                    mHandler.removeMessages(0);
                }
            }
        }, TIME);
//        mHandler.sendEmptyMessageDelayed(0,TIME);

        vp_banner_topic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                handler.removeCallbacksAndMessages(null);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(vp_banner_topic != null){
                            vp_banner_topic.setCurrentItem(vp_banner_topic.getCurrentItem()+1);
//                    handler.sendEmptyMessageDelayed(0,TIME);
                        }else{
//                    mHandler.removeMessages(0);
                        }
                    }
                }, TIME);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                notifyDots();
            }
        });
    }

    public void setData(List<RecommendData.DataBean.BannerBean> list){
        if(mTopicBannerPagerAdapter != null){
            mTopicBannerPagerAdapter.setList(list);
        }
        if(list != null){
//            mDataList = list;
            initDots(list.size());
        }
    }

    public TopicBannerPagerAdapter getBannerAdapter(){
        return mTopicBannerPagerAdapter;
    }

    private static class MyHandler extends  Handler{

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    //TODO 添加removeMessage的操作，避免内存泄漏
//                    if(vp_banner_topic != null){
//                        vp_banner_topic.setCurrentItem(vp_banner_topic.getCurrentItem()+1);
////                        mHandler.sendEmptyMessageDelayed(0,TIME);
//                    }else{
////                        mHandler.removeMessages(0);
//                    }
                    break;
                default:
                    break;
            }
        }
    }

//    private Handler mHandler =  new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what){
//                case 0:
//                    //TODO 添加removeMessage的操作，避免内存泄漏
//                    if(vp_banner_topic != null){
//                        vp_banner_topic.setCurrentItem(vp_banner_topic.getCurrentItem()+1);
//                        mHandler.sendEmptyMessageDelayed(0,TIME);
//                    }else{
//                        mHandler.removeMessages(0);
//                    }
//                    break;
//                default:
//                    break;
//            }
//        }
//    };

    private void initDots(int count) {
        if (ll_dots_container == null) {
            ll_dots_container = (LinearLayout) mContainer.findViewById(R.id.ll_dots_container);
        }

        ll_dots_container.removeAllViews();

        if(count<=1){
            ll_dots_container.setVisibility(View.GONE);
            return;
        }

        mDotsList = new ArrayList<>();
        ImageView firstDot = new ImageView(mContext);
        firstDot.setImageResource(R.drawable.point_choose);
        mDotsList.add(firstDot);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = UIUtils.getDimens(R.dimen.banner_dot_spacing);
        for (int i = 0; i < count - 1; i++) {
            ImageView temp = new ImageView(mContext);
            temp.setImageResource(R.drawable.point_normal);
            temp.setLayoutParams(lp);
            mDotsList.add(temp);
        }

        ll_dots_container.setVisibility(View.VISIBLE);
        for (View dot : mDotsList) {
            ll_dots_container.addView(dot);
        }
        notifyDots();
    }

    private void notifyDots() {
        if (mDotsList == null || mDotsList.size() == 0 || vp_banner_topic == null)
            return;
        for (ImageView dot : mDotsList) {
            dot.setImageResource(R.drawable.point_normal);
        }
        mDotsList.get(vp_banner_topic.getCurrentItem() % mDotsList.size()).setImageResource(R.drawable.point_choose);
    }
}
